/**
 * Created by ZHOUSH on 11/30/2015.
 */
var fs = require('fs');
var read = function (conf) {
    console.log(conf);
    return require('../config/' + conf);
};
exports.read = read;
exports.use = function (sys, db, acc) {
    var _sys = read('db_env.json').System
        .filter(function (item) {
            return item.Name == sys;
        });
    if (!_sys[0]) throw "ERROR_DB_CONN_NO_SYS: " + sys;
    var _db = _sys[0].DB.filter(function (item) {
        return item.Name == db;
    });
    if (!_db[0]) throw "ERROR_DB_CONN_NO_DB: " + db;
    var _acc = _db[0].Account.filter(function (item) {
        return item.Name == acc;
    });
    if (!_acc[0]) throw "ERROR_DB_CONN_NO_ACCOUNT: " + acc;
    return {
        connectString: '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = ' + _db[0].Host + ')(PORT = ' + _db[0].Port + '))' +
        '(CONNECT_DATA = (SERVER=DEDICATED) (SID = ' + _db[0].SID[0] + ')))',
        user: _acc[0].User,
        password: _acc[0].Password
    };
};
exports.all = function () {
    var ret = [];
    read('db_env.json').System.forEach(function (sys) {
        sys.DB.forEach(function (db) {
            db.Account.forEach(function (acc) {
                ret.push({
                    system: sys.Name,
                    db: db.Name,
                    account: acc.Name,
                    detail: acc.User + '@' + db.Host + ':' + db.Port + '/' + db.SID[0]
                })
            })
        })
    });
    return ret;
};